﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using LuaCEGUI.Controls;

namespace LuaCEGUI.XML
{
    class Parser
    {
        private XmlDocument _xml = new XmlDocument();
        private XmlElement _doc = null; //Form
        private XmlNodeList _nodelist = null;   //All Controls

        public Parser(string file)
        {
            if (System.IO.File.Exists(file))
            {
                Init(file);
            }
        }

        private int Init(string file)
        {
            _xml.Load(file);
            _doc = _xml.DocumentElement;
            _nodelist = _doc.ChildNodes;

            return 1;
        }

        private Dictionary<string,string> GetFormAttributeAll()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            XmlAttributeCollection xa = _doc.Attributes;
            int i;
            for (i = 0; i < xa.Count; i++)
            {
                d.Add(xa[i].Name, xa[i].Value);
            }
            return d;
        }

        public List<string> GetControlNameList()
        {
            List<string> l = new List<string>();
            int i;
            for (i = 0; i < _nodelist.Count; i++)
            {
                try
                {
                    l.Add(_nodelist[i].Attributes["Name"].Value);
                }
                catch
                {
                }
            }
            return l;
        }

        private Dictionary<string, string> GetControlAttributeAll(string name)
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            int i;
            for (i = 0; i < _nodelist.Count; i++)
            {
            if(_nodelist[i].Attributes["Name"].Value == name)
                try
                {
                    int j;
                    XmlAttributeCollection ac = _nodelist[i].Attributes;
                    d.Add("_ControlType", _nodelist[i].Name);
                    for (j = 0; j < ac.Count; j++)
                    {
                        d.Add(ac[j].Name, ac[j].Value);
                    }
                    break;
                }
                catch
                {
                }
            }
            return d;
        }

        public string GetControlAttribute(string ControlName, string AttributeName)
        {
            string type = (ControlName == GetFormName())?"Form":null;
            if (type == "Form")
            {
                Dictionary<string, string> d = GetFormAttributeAll();
                string value = null;
                try
                {
                    value = d[AttributeName];
                }
                catch
                {
                }
                return value;
            }
            else
            {
                Dictionary<string, string> d = GetControlAttributeAll(ControlName);
                string value = null;
                try
                {
                    value = d[AttributeName];
                }
                catch
                {
                }
                return value;
            }
        }

        public string GetFormName()
        {
            Dictionary<string, string> d = GetFormAttributeAll();
            string value = null;
            try
            {
                value = d["Name"];
            }
            catch
            {
            }
            return value;
        }
    }
}
